<title>修改密码</title>
<div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
        <a lay-href="">主页</a>
        <a><cite>设置</cite></a>
        <a><cite>我的密码</cite></a>
    </div>
</div>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-header">修改密码</div>
        <div class="layui-card-body" pad15>
            <form class="layui-form layui-form-pane" lay-filter="LAY-user-password-form">
                <div class="layui-form-item">
                    <label class="layui-form-label must">当前密码</label>
                    <div class="layui-input-block" style="width: 380px;">
                        <input type="password" name="old_password" lay-verify="required|pass" placeholder="请输入当前密码" lay-vertype="tips"  class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label must">新密码</label>
                    <div class="layui-input-block" style="width: 380px;">
                        <input type="password" name="password" placeholder="请输入新密码 8到16个字符" lay-verify="required|pass" lay-vertype="tips"  class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label must">确认新密码</label>
                    <div class="layui-input-block" style="width: 380px;">
                        <input type="password" name="password_confirmation" placeholder="请输入新密码 8到16个字符" lay-verify="required|pass_confirm" lay-vertype="tips" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button type="button" class="layui-btn" lay-submit lay-filter="LAY-user-password-formSubmit"><i class="layui-icon layui-icon-release"></i> 提 交</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
<script>
    layui.use(['index', 'baseMethods'], function () {
        const {form, layer, baseMethods} = layui;
        form.verify({
            pass: [/^[\S]{8,16}$/, '密码必须8到16位，且不能出现空格'],
            pass_confirm: function (value, item) {
                const data = form.val("formExample");
                if(!new RegExp(/^[\S]{8,16}$/).test(value)) {
                    return '密码必须8到16位，且不能出现空格';
                }
                if (data.password !== value) {
                    return '两次输入密码不一致';
                }
                if (data.old_password === value) {
                    return '不能使用原始密码';
                }
            },
        });
        form.on('submit(LAY-user-password-formSubmit)', function(data) {
            layer.confirm('确定提交么？', function() {
                baseMethods.request({
                    url: '/basic/password',
                    type: 'post',
                    data: data.field,
                    done: function (result) {
                        result.code === 200 ? layer.msg(result.message, {icon: 6}) : layer.msg(result.message, {icon: 5});
                    }
                })
            });
            return false;
        });
    });
</script>
